package com.heima.user.service.impl;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.feign.article.ArticleFilent;
import com.heima.feign.wemedia.WemediaFilent;
import com.heima.model.article.dtos.AuthorDto;
import com.heima.model.media.dtos.WmTypeEnum;
import com.heima.model.media.dtos.WmUserDto;
import com.heima.model.user.dtos.*;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserAuth;
import com.heima.user.mapper.ApUserAuthMapper;
import com.heima.user.service.ApUserAuthService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.user.service.ApUserService;
import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.models.auth.In;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;

/**
 * <p>
 * APP实名认证信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-02-01
 */
@Service
@Slf4j
public class ApUserAuthServiceImpl extends ServiceImpl<ApUserAuthMapper, ApUserAuth> implements ApUserAuthService {
    /**
     * 审核信息的分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult pageList(AutoStatusDto dto) {
        LambdaQueryWrapper<ApUserAuth> lqw = new LambdaQueryWrapper<>();
        lqw.eq(dto.getStatus()!=null,ApUserAuth::getStatus,dto.getStatus());
        Page<ApUserAuth> authPage = new Page<>(dto.getPage(),dto.getSize());
        Page<ApUserAuth> userAuthPage = this.page(authPage, lqw);
        //
        if (CollectionUtils.isEmpty(userAuthPage.getRecords())){
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return new PageResult(dto.getPage(), dto.getSize(), userAuthPage.getTotal(),userAuthPage.getRecords());
    }

    /**
     * 审核不通过
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authFail(AuthFailDto dto) {
        ApUserAuth apUserAuth = this.getById(dto.getId());
        if (apUserAuth == null){
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        updateAndInfo(dto.getId(),AuthStatus.FAIL.getCode(),dto.getMsg());

        return ResponseResult.ok();
    }

    private void updateAndInfo(Integer userAuthId, Integer status,String reason) {
        ApUserAuth userAuth = new ApUserAuth();
        userAuth.setId(userAuthId);
        userAuth.setStatus(status);
        userAuth.setReason(reason);
        userAuth.setUpdatedTime(new Date());
        this.updateById(userAuth);
    }


    @Autowired
    private ApUserService apUserService;

    @Autowired
    private ArticleFilent articleFilent;

    @Autowired
    private WemediaFilent wemediaFilent;

    /**
     * 审核通过
     * @param dto
     * @return
     */
    @Override
//    @Transactional
    @GlobalTransactional
    public ResponseResult authPass(AuthDto dto) {
        ApUserAuth apUserAuth = this.getById(dto.getId());
        if (apUserAuth == null){
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //查询ap_user
        ApUser apUser = apUserService.getById(apUserAuth.getUserId());
        if (apUser == null){
            throw new LeadException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //认证成功  修改状态
        log.info("修改的认证记录表");
        updateAndInfo(dto.getId(),AuthStatus.PASS.getCode(), "");
        //修改ap_user的身份、状态
        log.info("修改ap_user的身份、状态");
        apUserService.authSucc(apUser.getId());
        // 创建自媒体用户
        log.info("创建自媒体用户");

        WmUserDto userDto = buildWmUserDto(apUser);
        Integer wmUserId = wemediaFilent.createAndUpdateWmUser(userDto);

        // 创建文章的作者
        log.info("创建文章的作者");
        AuthorDto authorDto = buildAuthor(apUser, wmUserId);
        articleFilent.createAuthor(authorDto);

        return ResponseResult.ok();
    }

    private AuthorDto buildAuthor(ApUser apUser, Integer wmUserId) {
        AuthorDto authorDto = new AuthorDto();
        authorDto.setName(apUser.getName());
        authorDto.setWmUserId(wmUserId);
        authorDto.setUserId(apUser.getId());
        authorDto.setType(WmTypeEnum.ZI.getCode());
        return authorDto;
    }

    private WmUserDto buildWmUserDto(ApUser apUser) {
        WmUserDto userDto = new WmUserDto();
        userDto.setName(apUser.getName());
        userDto.setPassword(apUser.getPassword());
        userDto.setApUserId(apUser.getId());
        userDto.setStatus(AuthStatus.PASS.getCode());
        userDto.setPhone(apUser.getPhone());
        return userDto;
    }
}
